package cn.enilu.flash.api.controller.h5;

import cn.enilu.flash.api.utils.BaiduMapUtils;
import cn.enilu.flash.bean.entity.cms.Banner;
import cn.enilu.flash.bean.entity.task.BaiduCityCode;
import cn.enilu.flash.bean.entity.task.Classify;
import cn.enilu.flash.bean.entity.task.HelpTask;
import cn.enilu.flash.bean.vo.front.Ret;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.offcialsite.BannerVo;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.task.ClassifyVo;
import cn.enilu.flash.bean.vo.task.HelpTaskListVo;
import cn.enilu.flash.service.cms.BannerService;
import cn.enilu.flash.service.task.BaiduCityCodeService;
import cn.enilu.flash.service.task.ClassifyService;
import cn.enilu.flash.service.task.HelpTaskService;
import cn.enilu.flash.utils.HttpUtil;
import cn.enilu.flash.utils.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/h5/index")
public class IndexController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private HelpTaskService helpTaskService;
    @Autowired
    private BaiduCityCodeService baiduCityCodeService;
    @GetMapping("getIndexInfo")
    public Ret getIndexInfo(){
        Map<String,Object> resultMap = new LinkedHashMap<>();
        Integer cityCode = null;
        // 获取城市 start
        String ip = HttpUtil.getIp();
        if(ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1") || ip.equals("192.168.1.250") || ip.equals("192.168.1.128")){
            ip = "219.159.239.17";// 设为崇左
        }
        Map<String,Object> addressMap = new LinkedHashMap<>();
        Map<String, Object> address = BaiduMapUtils.getAddress(ip);
        Map<String,Object> contentMap = (Map<String, Object>) address.get("content");
        Map<String,Object> addressDetailMap = (Map<String, Object>) contentMap.get("address_detail");
        cityCode = (Integer) addressDetailMap.get("city_code");
        String city = (String)addressDetailMap.get("city");
        addressMap.put("cityCode",cityCode);
        addressMap.put("city",city);
        // 获取城市 end

        // 获取 banners start
        List<Banner> banners = bannerService.queryAll();
        List<BannerVo> bannerList = new ArrayList<>();
        for(Banner banner:banners){
            BannerVo bannerVo = new BannerVo();
            BeanUtils.copyProperties(banner,bannerVo);
            bannerList.add(bannerVo);
        }
        resultMap.put("bannerList",bannerList);
        // 获取 banners end

        // 获取分类 start
        List<ClassifyVo> classifyList = new ArrayList<>();
        List<Classify> classifies = classifyService.queryAll();
        for(Classify classify:classifies){
            ClassifyVo classifyVo = new ClassifyVo();
            BeanUtils.copyProperties(classify,classifyVo);
            classifyList.add(classifyVo);
        }
        resultMap.put("classifyList",classifyList);
        // 获取分类 end

        // 排序类型  start
        List<Map<String,Object>> sortList = new ArrayList<>();
        Map<String,Object> sortMap;

        sortMap = new LinkedHashMap<>();
        sortMap.put("index",1);
        sortMap.put("name","综合");
        sortList.add(sortMap);

        sortMap = new LinkedHashMap<>();
        sortMap.put("index",2);
        sortMap.put("name","价格高-低");
        sortList.add(sortMap);

        sortMap = new LinkedHashMap<>();
        sortMap.put("index",3);
        sortMap.put("name","新发布");
        sortList.add(sortMap);
        resultMap.put("sortList",sortList);
        // 排序类型  end

        // 初始任务列表 start
        Ret taskList = getTaskList(null,cityCode,-1,1, 10,1);
        resultMap.put("taskList",taskList.getData());
        // 初始任务列表 end
        return Rets.success(resultMap);
    }
    @GetMapping("/getTaskList")
    public Ret getTaskList(String searchName,
                           Integer cityCode,
                           @RequestParam(defaultValue = "-1") Integer classify,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit,
                           @RequestParam(defaultValue = "1") Integer sortType){
        StringBuffer sql = new StringBuffer("Select * from t_help_task where state = 1 ");
        if(StringUtil.isNotNullOrEmpty(searchName)){
            sql.append("and title like '%"+searchName+"%' ");
        }
        if(StringUtil.isNotNullOrEmpty(cityCode)){
            sql.append("and city_id = "+cityCode+" ");
        }
        if(classify != -1){
            sql.append("and classify_id = "+classify+" ");
        }
        if(sortType == 1){
            sql.append(" order by (price/time_limit) desc ");
        }
        if(sortType == 2){
            sql.append(" order by price desc ");
        }
        if(sortType == 3){
            sql.append(" order by create_time desc ");
        }
        if(page<1) page = 1;
        int start = (page-1)*limit;
        sql.append("LIMIT "+start+","+limit+" ");
        List<HelpTask> tasks = helpTaskService.queryBySql(sql.toString());
        List<HelpTaskListVo> taskVoList = new ArrayList<>();
        for(HelpTask helpTask:tasks){
            HelpTaskListVo HelpTaskListVo = new HelpTaskListVo();
            BeanUtils.copyProperties(helpTask,HelpTaskListVo);
            taskVoList.add(HelpTaskListVo);
        }
        return Rets.success(taskVoList);
    }

    /**
     * 获取全部已开放城市信息
     * @return
     */
    @GetMapping("/getCityInfo")
    public Ret getCityInfo(@RequestParam(defaultValue = "") String city){
        List<BaiduCityCode> baiduCityCodes = null;
        List<SearchFilter> list = new ArrayList<>();
        list.add(new SearchFilter("isOpen", SearchFilter.Operator.EQ,1));
        if(StringUtil.isEmpty(city)){
            baiduCityCodes = baiduCityCodeService.queryAll(list);
        }else {
            list.add(new SearchFilter("city", SearchFilter.Operator.LIKE, city));
            baiduCityCodes = baiduCityCodeService.queryAll(list);
        }

        return Rets.success(baiduCityCodes);
    }

    /**
     * 获取用户当前城市信息
     * @return
     */
    @GetMapping("/getUserCityInfo")
    public Ret getCityCode(){
        String ip = HttpUtil.getIp();
        if(ip.equals("127.0.0.1") || ip.equals("0:0:0:0:0:0:0:1") || ip.equals("192.168.1.250")){
            ip = "219.159.239.17";// 设为崇左
        }
        Map<String,Object> addressMap = new LinkedHashMap<>();
        Map<String, Object> address = BaiduMapUtils.getAddress(ip);
        Map<String,Object> contentMap = (Map<String, Object>) address.get("content");
        Map<String,Object> addressDetailMap = (Map<String, Object>) contentMap.get("address_detail");
        Integer cityCode = (Integer) addressDetailMap.get("city_code");
        String city  = (String)addressDetailMap.get("city");
        addressMap.put("cityCode",cityCode);
        addressMap.put("city",city);
        return Rets.success(addressMap);
    }
    /**
     * 获取分类信息
     * @return
     */
    @GetMapping("/getClassify")
    @ApiOperation("获取所有分类")
    public Ret getClassify(){
        List<ClassifyVo> classifyList = new ArrayList<>();
        List<Classify> classifies = classifyService.queryAll();
        for(Classify classify:classifies){
            ClassifyVo classifyVo = new ClassifyVo();
            BeanUtils.copyProperties(classify,classifyVo);
            classifyList.add(classifyVo);
        }
        return Rets.success(classifyList);
    }

}
